﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;

namespace BLL
{
    public class Common
    {
        private Common() { }
        /// <summary>
        /// 获取安全参数，必须是int型
        /// </summary>
        /// <param name="page"></param>
        /// <param name="str">需要获取的参数</param>
        /// <returns></returns>
        public static string GetSafeRequest(Page page, string str)
        {
            string strReturn = string.Empty;
            try
            {
                strReturn = page.Request.QueryString[str].ToString();
                Convert.ToInt32(strReturn);
            }
            catch 
            {
                strReturn = "-1";
            }
            return strReturn;
        }
        public static string GetSafeRequest(Page page)
        {
            string strReturn = string.Empty;
            try
            {
                strReturn = page.Request.QueryString["ID"].ToString();
                Convert.ToInt32(strReturn);
            }
            catch
            {
                strReturn = "-1";
            }
            return strReturn;
        }
        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string SubFormat(String str,int len){
            if (str!="")
	        {
                if (str.Length>len)
	            {
                    str = str.Substring(0,len)+"...";
	            }
	        }
            return str;
        }
        /// <summary>
        /// 时间格式化yyyy-mm-dd
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string TimeFormat(string time){
            try
            {
                time = Convert.ToDateTime(time).ToString("yyyy-MM-dd");
                return time;
            }
            catch 
            {
                return time;
            }
        }
        //难度格式化
        public static string DiffFormat(string diff) {
            try
            {
                var a = Convert.ToInt32(diff);
                diff = a * 20 + "%";
                return diff;
            }
            catch 
            {
                return "20%";
            }
        }
    }
}
